import { useReactive } from "ahooks";
import { useModel } from "umi";
import { useEffect } from "react";
import compress from "gql-compress";

const useAllAffiliates = (oiType) => {
    const { graphqlFetch } = useModel("global");

    const state = useReactive({
        loading: false,
        affiliates: [],
    });

    // 加载所有部门信息, 用树的数据格式
    const loadAllAffiliates = async (oiType) => {
        state.loading = true;
        let res = await graphqlFetch({
            query,
            variables: {},
        });
        let affiliates = res?.a?.nodes || [];
        state.affiliates = affiliates;
        state.loading = false;
        return affiliates;
    };

    useEffect(() => {
        loadAllAffiliates(oiType);
    }, []);

    return { ...state, loadAllAffiliates };
};

const query = compress(`query MyQuery {
    a: allOrganizationAffiliates(condition: {oaStat: "正常"}) {
        nodes {
            oaUuid
            oiUuid
            oaName
        }
    }
}`);

export default useAllAffiliates;
